package triggerhelper;

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.plaf.basic.BasicBorders;
import triggerhelper.controllers.*;
import triggerhelper.dataHolders.*;
import triggerhelper.tabs.*;

public class triggerCreator extends javax.swing.JFrame {

    /** Creates new form TriggerCreator */
    public triggerCreator() {
        this.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e){
                mbController.exit();
            }
        });
        try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (UnsupportedLookAndFeelException e) {
                e.printStackTrace();
            }
        initComponents();
        initControllers();

        setLocationRelativeTo(null);
        
        //disable tabs
        for (int i=1; i<mainTabPane.getTabCount(); i++) {
            mainTabPane.setEnabledAt(i, false);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        lMainTitle = new javax.swing.JLabel();
        lMainProject = new javax.swing.JLabel();
        mainProject = new javax.swing.JLabel();
        lMainTrigger = new javax.swing.JLabel();
        mainTrigger = new javax.swing.JLabel();
        mainTabPane = new javax.swing.JTabbedPane();
        triggerDataTab = new triggerhelper.tabs.TriggerDataTab();
        variablesTab = new triggerhelper.tabs.VariablesTab();
        conditionsTab = new triggerhelper.tabs.ConditionsTab();
        actionsTab = new triggerhelper.tabs.ActionsTab();
        finalTab = new triggerhelper.tabs.FinalTab();
        mainMenuBar = new javax.swing.JMenuBar();
        menuFile = new javax.swing.JMenu();
        fmenuNew = new javax.swing.JMenuItem();
        fmenuOpen = new javax.swing.JMenuItem();
        fmenuSave = new javax.swing.JMenuItem();
        fmenuSaveAs = new javax.swing.JMenuItem();
        fmenuExit = new javax.swing.JMenuItem();
        menuEdit = new javax.swing.JMenu();
        emenuOpen = new javax.swing.JMenuItem();
        emenuSave = new javax.swing.JMenuItem();
        emenuSaveAs = new javax.swing.JMenuItem();
        menuHelp = new javax.swing.JMenu();
        hmenuReadMe = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Trigger Helper");
        setMinimumSize(new java.awt.Dimension(418, 392));

        mainPanel.setBackground(new java.awt.Color(219, 228, 255));
        mainPanel.setForeground(new java.awt.Color(219, 228, 255));
        mainPanel.setMinimumSize(new java.awt.Dimension(410, 337));

        lMainTitle.setFont(new java.awt.Font("Calibri", 1, 24));
        lMainTitle.setForeground(new java.awt.Color(46, 57, 96));
        lMainTitle.setText("Trigger Helper");

        lMainProject.setFont(new java.awt.Font("Calibri", 0, 14));
        lMainProject.setForeground(new java.awt.Color(46, 57, 96));
        lMainProject.setText("Project: ");

        mainProject.setFont(new java.awt.Font("Calibri", 0, 14));
        mainProject.setForeground(new java.awt.Color(46, 57, 96));
        mainProject.setText("<project>");

        lMainTrigger.setFont(new java.awt.Font("Calibri", 0, 14));
        lMainTrigger.setForeground(new java.awt.Color(46, 57, 96));
        lMainTrigger.setText("Trigger:");

        mainTrigger.setFont(new java.awt.Font("Calibri", 0, 14));
        mainTrigger.setForeground(new java.awt.Color(46, 57, 96));
        mainTrigger.setText("<trigger>");

        mainTabPane.setBackground(new java.awt.Color(255, 223, 223));
        mainTabPane.setFocusable(false);
        mainTabPane.addTab("Trigger Data", triggerDataTab);
        mainTabPane.addTab("Variables", variablesTab);
        mainTabPane.addTab("Conditions", conditionsTab);
        mainTabPane.addTab("Actions", actionsTab);
        mainTabPane.addTab("Final", finalTab);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(mainTabPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(lMainTitle)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 140, Short.MAX_VALUE)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(lMainTrigger)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(mainTrigger))
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(lMainProject)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(mainProject)))))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lMainTitle)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(mainProject)
                            .addComponent(lMainProject))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lMainTrigger)
                            .addComponent(mainTrigger))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(mainTabPane, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                .addContainerGap())
        );

        mainMenuBar.setBackground(new java.awt.Color(219, 228, 255));
        mainMenuBar.setBorder(new BasicBorders.MenuBarBorder(new Color(219, 228, 255), new Color(46,57,96)));
        mainMenuBar.setMaximumSize(new java.awt.Dimension(129, 21));
        mainMenuBar.setMinimumSize(new java.awt.Dimension(129, 21));

        menuFile.setBackground(new java.awt.Color(219, 228, 255));
        menuFile.setForeground(new java.awt.Color(46, 57, 96));
        menuFile.setText("File");

        fmenuNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        fmenuNew.setBackground(new java.awt.Color(255, 255, 228));
        fmenuNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/newProject.png"))); // NOI18N
        fmenuNew.setText("New project");
        fmenuNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fmenuNewActionPerformed(evt);
            }
        });
        menuFile.add(fmenuNew);

        fmenuOpen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        fmenuOpen.setBackground(new java.awt.Color(255, 255, 228));
        fmenuOpen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/open.png"))); // NOI18N
        fmenuOpen.setText("Open project");
        fmenuOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fmenuOpenActionPerformed(evt);
            }
        });
        menuFile.add(fmenuOpen);

        fmenuSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        fmenuSave.setBackground(new java.awt.Color(255, 255, 228));
        fmenuSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/save.png"))); // NOI18N
        fmenuSave.setText("Save");
        fmenuSave.setEnabled(false);
        fmenuSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fmenuSaveActionPerformed(evt);
            }
        });
        menuFile.add(fmenuSave);

        fmenuSaveAs.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        fmenuSaveAs.setBackground(new java.awt.Color(255, 255, 228));
        fmenuSaveAs.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/saveAs.png"))); // NOI18N
        fmenuSaveAs.setText("Save as");
        fmenuSaveAs.setEnabled(false);
        fmenuSaveAs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fmenuSaveAsActionPerformed(evt);
            }
        });
        menuFile.add(fmenuSaveAs);

        fmenuExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        fmenuExit.setBackground(new java.awt.Color(255, 255, 228));
        fmenuExit.setText("Exit");
        fmenuExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fmenuExitActionPerformed(evt);
            }
        });
        menuFile.add(fmenuExit);

        mainMenuBar.add(menuFile);

        menuEdit.setBackground(new java.awt.Color(219, 228, 255));
        menuEdit.setForeground(new java.awt.Color(46, 57, 96));
        menuEdit.setText("Edit Project");

        emenuOpen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        emenuOpen.setBackground(new java.awt.Color(255, 255, 228));
        emenuOpen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/newTrigger.png"))); // NOI18N
        emenuOpen.setText("Import trigger");
        emenuOpen.setEnabled(false);
        emenuOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emenuOpenActionPerformed(evt);
            }
        });
        menuEdit.add(emenuOpen);

        emenuSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        emenuSave.setBackground(new java.awt.Color(255, 255, 228));
        emenuSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/saveTrigger.png"))); // NOI18N
        emenuSave.setText("Save trigger");
        emenuSave.setEnabled(false);
        emenuSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emenuSaveActionPerformed(evt);
            }
        });
        menuEdit.add(emenuSave);

        emenuSaveAs.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        emenuSaveAs.setBackground(new java.awt.Color(255, 255, 228));
        emenuSaveAs.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/saveAsTrigger.png"))); // NOI18N
        emenuSaveAs.setText("Save trigger as");
        emenuSaveAs.setEnabled(false);
        emenuSaveAs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emenuSaveAsActionPerformed(evt);
            }
        });
        menuEdit.add(emenuSaveAs);

        mainMenuBar.add(menuEdit);

        menuHelp.setBackground(new java.awt.Color(219, 228, 255));
        menuHelp.setForeground(new java.awt.Color(46, 57, 96));
        menuHelp.setText("Help");

        hmenuReadMe.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.ALT_MASK));
        hmenuReadMe.setBackground(new java.awt.Color(255, 255, 228));
        hmenuReadMe.setText("View readMe");
        menuHelp.add(hmenuReadMe);

        mainMenuBar.add(menuHelp);

        setJMenuBar(mainMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //***************
    //Action handlers
    //***************
    private void fmenuNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fmenuNewActionPerformed
        mbController.newProject();
    }//GEN-LAST:event_fmenuNewActionPerformed
    private void fmenuOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fmenuOpenActionPerformed
        mbController.openProject();
    }//GEN-LAST:event_fmenuOpenActionPerformed
    private void fmenuSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fmenuSaveActionPerformed
        mbController.saveProject();
    }//GEN-LAST:event_fmenuSaveActionPerformed
    private void fmenuSaveAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fmenuSaveAsActionPerformed
        mbController.saveProjectAs();
    }//GEN-LAST:event_fmenuSaveAsActionPerformed
    private void fmenuExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fmenuExitActionPerformed
        mbController.exit();
    }//GEN-LAST:event_fmenuExitActionPerformed
    private void emenuOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emenuOpenActionPerformed
        mbController.importTrigger();
    }//GEN-LAST:event_emenuOpenActionPerformed
    private void emenuSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emenuSaveActionPerformed
        mbController.saveTrigger();
    }//GEN-LAST:event_emenuSaveActionPerformed
    private void emenuSaveAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emenuSaveAsActionPerformed
        mbController.saveTriggerAs();
    }//GEN-LAST:event_emenuSaveAsActionPerformed

    //**********************
    //End of action handlers
    //**********************

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                triggerCreator tc = new triggerCreator();
                tc.setVisible(true);
            }
        });
    }

    //******************************
    //Personal variable declarations
    //******************************
    private MenuBarController mbController;
    private VariableController vController;
    private TriggerDataController tdController;
    private ConditionsController cController;
    private ActionsController aController;
    private FinalController fController;
    private TriggerProject currentProject;
    private SimpleTrigger currentTrigger;

    //Getter methods
    public JTabbedPane getMainTabPane() {
        return mainTabPane;
    }public JLabel getMainProject() {
        return mainProject;
    }public JLabel getMainTrigger() {
        return mainTrigger;
    }public TriggerProject getCurrentProject() {
        return currentProject;
    }public SimpleTrigger getCurrentTrigger() {
        return currentTrigger;
    }public JList getListTriggers() {
        return triggerDataTab.getListTriggers();
    }public JTextField getfTrigName() {
        return triggerDataTab.getfTrigName();
    }public VariablesTab getVariablesTab() {
        return variablesTab;
    }public MenuBarController getMenuBarController() {
        return mbController;
    }public JMenuItem getEmenuOpen() {
        return emenuOpen;
    }public JMenuItem getEmenuSave() {
        return emenuSave;
    }public JMenuItem getEmenuSaveAs() {
        return emenuSaveAs;
    }public JMenuItem getFmenuSave() {
        return fmenuSave;
    }public JMenuItem getFmenuSaveAs() {
        return fmenuSaveAs;
    }public TriggerDataController getTdController() {
        return tdController;
    }

    //Setter methods
    public void setCurrentProject(TriggerProject currentProject_) {
        currentProject = currentProject_;
    }public void setCurrentTrigger(SimpleTrigger currentTrigger_) {
        currentTrigger = currentTrigger_;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private triggerhelper.tabs.ActionsTab actionsTab;
    private triggerhelper.tabs.ConditionsTab conditionsTab;
    private javax.swing.JMenuItem emenuOpen;
    private javax.swing.JMenuItem emenuSave;
    private javax.swing.JMenuItem emenuSaveAs;
    private triggerhelper.tabs.FinalTab finalTab;
    private javax.swing.JMenuItem fmenuExit;
    private javax.swing.JMenuItem fmenuNew;
    private javax.swing.JMenuItem fmenuOpen;
    private javax.swing.JMenuItem fmenuSave;
    private javax.swing.JMenuItem fmenuSaveAs;
    private javax.swing.JMenuItem hmenuReadMe;
    private javax.swing.JLabel lMainProject;
    private javax.swing.JLabel lMainTitle;
    private javax.swing.JLabel lMainTrigger;
    private javax.swing.JMenuBar mainMenuBar;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JLabel mainProject;
    private javax.swing.JTabbedPane mainTabPane;
    private javax.swing.JLabel mainTrigger;
    private javax.swing.JMenu menuEdit;
    private javax.swing.JMenu menuFile;
    private javax.swing.JMenu menuHelp;
    private triggerhelper.tabs.TriggerDataTab triggerDataTab;
    private triggerhelper.tabs.VariablesTab variablesTab;
    // End of variables declaration//GEN-END:variables

    private void initControllers() {
        mbController = new MenuBarController(this);
        tdController = new TriggerDataController(triggerDataTab, this);
        vController = new VariableController(variablesTab, this);
        cController = new ConditionsController(conditionsTab, this);
        aController = new ActionsController(actionsTab, this);
        fController = new FinalController(finalTab, this);
    }


}
